home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 204_01 / func.c < prev    next >
Text File  |  1980-01-01  |  4KB  |  123 lines

  1. #include        <stdio.h>
  2. #include        "c.h"
  3. #include        "expr.h"
  4. #include        "gen.h"
  5. #include        "cglbdec.h"
  6.  
  7. /*
  8.  *    68000 C compiler
  9.  *
  10.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  11.  *  all commercial rights reserved.
  12.  *
  13.  *    This compiler is intended as an instructive tool for personal use. Any
  14.  *    use for profit without the written consent of the author is prohibited.
  15.  *
  16.  *    This compiler may be distributed freely for non-commercial use as long
  17.  *    as this notice stays intact. Please forward any enhancements or questions
  18.  *    to:
  19.  *
  20.  *        Matthew Brandt
  21.  *        Box 920337
  22.  *        Norcross, Ga 30092
  23.  */
  24.  
  25. /*      function compilation routines           */
  26.  
  27. funcbody(sp)
  28. /*
  29.  *      funcbody starts with the current symbol being either
  30.  *      the first parameter id or the begin for the local
  31.  *      block. If begin is the current symbol then funcbody
  32.  *      assumes that the function has no parameters.
  33.  */
  34. SYM     *sp;
  35. {       char    *names[20];             /* 20 parameters maximum */
  36.         int     nparms, poffset, i;
  37.         SYM     *sp1, *makeint();
  38.         global_flag = 0;
  39.         poffset = 8;            /* size of return block */
  40.         nparms = 0;
  41.         if(lastst == id) {              /* declare parameters */
  42.                 while(lastst == id) {
  43.                         names[nparms++] = litlate(lastid);
  44.                         getsym();
  45.                         if( lastst == comma)
  46.                                 getsym();
  47.                         else
  48.                                 break;
  49.                         }
  50.                 needpunc(closepa);
  51.                 dodecl(sc_member);      /* declare parameters */
  52.                 for(i = 0;i < nparms;++i) {
  53.                         if( (sp1 = search(names[i],lsyms.head)) == 0)
  54.                                 sp1 = makeint(names[i]);
  55.                         if( sp1->tp->size < 4 )
  56.                         {
  57.                             sp1->value.i = poffset + (4 - sp1->tp->size);
  58.                             poffset += 4;
  59.                         }
  60.                         else
  61.                         {
  62.                             sp1->value.i = poffset;
  63.                             poffset += sp1->tp->size;
  64.                         }
  65.                         sp1->storage_class = sc_auto;
  66.                     }
  67.                 }
  68.         if(lastst != begin)
  69.                 error(ERR_BLOCK);
  70.         else    {
  71.                 cseg();
  72.                 gen_strlab(sp->name);
  73.                 block();
  74.                 funcbottom();
  75.                 }
  76.         global_flag = 1;
  77. }
  78.  
  79. SYM     *makeint(name)
  80. char    *name;
  81. {       SYM     *sp;
  82.         TYP     *tp;
  83.         sp = xalloc(sizeof(SYM));
  84.         tp = xalloc(sizeof(TYP));
  85.         tp->type = bt_long;
  86.         tp->size = 4;
  87.         tp->btp = tp->lst.head = 0;
  88.         tp->sname = 0;
  89.         sp->name = name;
  90.         sp->storage_class = sc_auto;
  91.         sp->tp = tp;
  92.         insert(sp,&lsyms);
  93.         return sp;
  94. }
  95.  
  96. check_table(head)
  97. SYM     *head;
  98. {       while( head != 0 ) {
  99.                 if( head->storage_class == sc_ulabel )
  100.                         fprintf(list,"*** UNDEFINED LABEL - %s\n",head->name);
  101.                 head = head->next;
  102.                 }
  103. }
  104.  
  105. funcbottom()
  106. {       nl();
  107.         check_table(lsyms.head);
  108.         lc_auto = 0;
  109.         fprintf(list,"\n\n*** local symbol table ***\n\n");
  110.         list_table(&lsyms,0);
  111.         fprintf(list,"\n\n\n");
  112.         release_local();        /* release local symbols */
  113. }
  114.  
  115. block()
  116. {       needpunc(begin);
  117.         dodecl(sc_auto);
  118.         cseg();
  119.         genfunc(compound());
  120.         flush_peep();
  121. }
  122.  
  123.